import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
//引入路径
import path from 'path'
//引入SVG需要用到的插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
//引入mock插件提供的方法
import { viteMockServe } from 'vite-plugin-mock'
//引入组件命别名插件
import VueSetupExtend from 'vite-plugin-vue-setup-extend'

export default defineConfig(({ command, mode }) => {
  //获取各种环境下的环境变量
  let env = loadEnv(mode, process.cwd())
  //返回配置对象
  return {
    //配置svg图标插件;mock插件;插件命别名
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        enable: command === 'serve',
      }),
      VueSetupExtend(),
    ],
    //配置相对路径别名，使用 @ 代替 src
    resolve: {
      alias: {
        '@': path.resolve('./src'),
      },
    },
    //配置一个scss全局变量
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/styles/variable.scss" as *;',
        },
      },
    },
    //配置代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE, //获取数据的服务器地址设置
          changeOrigin: true, //是否代理跨域
          rewrite: (path) => path.replace(/^\/api/, ''), //路径重写
        },
      },
    },
  }
})
